#include <func.h>
#include "head.h"

int findIdByfileName(MYSQL *db, char *filePath){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "select id from catalog where file_name = '%s';", filePath);
    //puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
        return -1;
    }
    MYSQL_RES *result = mysql_store_result(db);
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        return -1;
    }
    char fileId[100];
    strcpy(fileId, row[0]);
    mysql_free_result(result);
    return atoi(fileId);
}

int findMD5ByfilePath(MYSQL *db, char *filePath,char *md5){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "select md5 from catalog where file_name = '%s';", filePath);
    //puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
        return -1;
    }
    MYSQL_RES *result = mysql_store_result(db);
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        return -1;
    }
    strcpy(md5, row[0]);
    mysql_free_result(result);
    return 0;
}

int insertDir(MYSQL *db, char *filePath, char *userName, int curPathId){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "insert into catalog (file_name, usr_name, pre_id) values ('%s', '%s', %d);", filePath, userName, curPathId);
    // puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
        return -1;
    }
    return 0;
}

int findChildBycurPathId(MYSQL *db, int curPathId, char *childrenName){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "select file_name from catalog where pre_id = %d;", curPathId);
    //puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
        return -1;
    }
    MYSQL_RES *result = mysql_store_result(db);
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        return -1;
    }
    do{
        char childPath[200] = {0};
        strcpy(childPath, row[0]);
        char childName[100] = {0};
        getFileName(childName, childPath);
        strcat(childrenName, childName);
        strcat(childrenName, " ");
    }while((row = mysql_fetch_row(result)) != NULL);
    return 0;
}

int findParentIdByfileName(MYSQL *db, char *curPath){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "select pre_id from catalog where file_name = '%s';", curPath);
    // puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
        return -1;
    }
    MYSQL_RES *result = mysql_store_result(db);
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        return -1;
    }
    return atoi(row[0]);
}

int findFileNameById(MYSQL *db, int id, char *filePath){
    char sqlCommand[1024] = {0};
    // puts("pre_id");
    sprintf(sqlCommand, "select file_name from catalog where id = %d;", id);
    //puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    // printf("qret = %d\n", qret);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
        return -1;
    }
    MYSQL_RES *result = mysql_store_result(db);
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        // puts("row");
        return -1;
    }
    strcpy(filePath, row[0]);
    return 0;
}

int isDirectoryByfileName(MYSQL *db, char *fileName, char *userName){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "select file_name from catalog where file_name = '%s' and md5 is NULL and usr_name = '%s';", fileName, userName);
    //puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
    }
    MYSQL_RES *result = mysql_store_result(db);
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        return -1;
    }
    return 0;
}

int isFileByfileName(MYSQL *db, char *fileName, char *userName){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "select * from catalog where file_name = '%s' and md5 is not NULL and usr_name = '%s';", fileName, userName);
    // puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
    }
    MYSQL_RES *result = mysql_store_result(db);
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        return -1;
    }
    return 0;
}

int deleteFilebyfileName(MYSQL *db, char *fileName, char *userName){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "delete from catalog where file_name = '%s' and md5 is not NULL and usr_name = '%s';", fileName, userName);
    //puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
        return -1;
    }
    return 0;
}

int deletebyid(MYSQL *db, int curPathId){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "delete from catalog where id = %d;", curPathId);
    //puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
        return -1;
    }
    return 0;
}

int insertNewUser(MYSQL *db, char *userName,  char *salt, char *cryptograph, char *token){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "insert into user_login_info(usr_name, salt, crypt, token) values('%s', '%s', '%s', '%s');", userName, salt, cryptograph, token);
    // puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
        return -1;
    }
    return 0;
}

int insertNewUserRootDir(MYSQL *db, char *userName){
    char sqlCommand[1024] = {0};
    char path[100] = {0};
    sprintf(path, "%s%s", "/", userName);
    sprintf(sqlCommand, "insert into catalog(file_name, usr_name, pre_id) values('%s', '%s', %d);", path, userName, 0);
    // puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
        return -1;
    }
    return 0;
}

int isUserBytoken(MYSQL *db, char *userName, char *token){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "select token from user_login_info where usr_name = '%s';", userName);
    // puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
    }
    MYSQL_RES *result = mysql_store_result(db);
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        return -1;
    }
    if(strcmp(row[0], token) == 0){
        //token匹配
        return 1;
    }else{
        return 0;
    }
}

int updateTokenInUserTable(MYSQL *db, char *userName, char *token){
    char sqlCommand[1024] = {0};
    sprintf(sqlCommand, "update usr_login_info set token = '%s' where usr_name = '%s';", token, userName);
    // puts(sqlCommand);
    int qret = mysql_query(db, sqlCommand);
    if(qret != 0){
        printf("Error: %s\n", mysql_error(db));
    }
}